package cn.yuanqiao.web.controller.archive.bianyan;

import cn.yuanqiao.archive.bianyan.domain.YqZhuanTi;
import cn.yuanqiao.archive.bianyan.domain.YqZhuanTiBlob;
import cn.yuanqiao.archive.bianyan.mapper.YqZhuanTiBlobMapper;
import cn.yuanqiao.archive.bianyan.service.IYqZhuanTiService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.system.domain.SysFile;
import cn.yuanqiao.common.core.context.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 创建专题库
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.web.controller.archive.bianyan
 * @ClassName: YqZhuanTiController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/5 9:29
 * @Version: 1.0
 */
@RestController
@RequestMapping("/archive/special/subject")
public class YqZhuanTiController extends BaseController {
    @Autowired
    private IYqZhuanTiService IYqZhuanTiService;

    @Autowired
    private YqZhuanTiBlobMapper yqZhuanTiBlobMapper;

    /**
     * 查询专题列表
     */
//    @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:list')")
    @GetMapping("/list")
    public AjaxResult list(YqZhuanTi YqZhuanTi) {
        startPage();
        List<YqZhuanTi> list = IYqZhuanTiService.selectYqZhuanTiList(YqZhuanTi);
        return success(list);
    }

    /**
     * 获取专题详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") String typeId) {
        return success(IYqZhuanTiService.selectYqZhuanTiByTypeId(typeId));
    }

    /**
     * 新增专题
     */
    @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:add')")
    @Log(title = "专题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqZhuanTi YqZhuanTi) {
        return IYqZhuanTiService.insertYqZhuanTi(YqZhuanTi);
    }


    /**
     * 新增专题明细
     */
    // @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:add')")
    @Log(title = "新增专题明细", businessType = BusinessType.INSERT)
    @PostMapping("/blob/add")
    public AjaxResult blobAdd(@RequestBody YqZhuanTiBlob yqZhuanTiBlob) {
        return toAjax(IYqZhuanTiService.insertYqZhuanTiBlob(yqZhuanTiBlob));
    }


    /**
     * 修改专题
     */
    @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:edit')")
    @Log(title = "专题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqZhuanTi YqZhuanTi) {
        return toAjax(IYqZhuanTiService.updateYqZhuanTi(YqZhuanTi));
    }

    /**
     * 删除专题
     */
    @PreAuthorize("@ss.hasPermi('archive:YqZhuanTi:remove')")
    @Log(title = "专题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable String typeId) {
        return toAjax(IYqZhuanTiService.deleteYqZhuanTiByTypeId(typeId));
    }

    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    R<SysFile> uploadPicture(MultipartFile file) {
        return IYqZhuanTiService.uploadPicture(file);
    }

    /**
     * 文件预览
     * @param response
     * @param id
     * @param type
     * @throws Exception
     */
    @PostMapping("/preview")
    public void view(HttpServletResponse response, @RequestParam("id") String id, @RequestParam("type") String type)
            throws Exception {
        byte[] bytes = IYqZhuanTiService.queryImage(id, type);
        // 以流的形式返回
        ServletOutputStream out = null;
        ByteArrayOutputStream baos = null;
        try {
            InputStream inStream = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.reset(); // 非常重要
            // 设置 CORS 响应头
            ResponseUtils.defaultResponse(response);
            //在线预览，浏览器支持的文件类型都能打开
            response.setContentType("application/octet-stream");
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Objects.requireNonNull(baos).flush();
            baos.close();
            Objects.requireNonNull(out).flush();
            out.close();
        }
    }


    /**
     * 删除上传文件
     *
     * @param filePath
     * @return
     */
    @PostMapping("/file/remove")
    public AjaxResult fileRemove(String filePath) {
        return toAjax(IYqZhuanTiService.fileDelete(filePath));
    }

    /**
     * 查询专题树
     *
     * @param
     * @return cn.yuanqiao.common.core.domain.AjaxResult
     * @author hbWang
     * @date 2023/7/5 10:19
     */
    @PostMapping("/tree")
    public AjaxResult tree(@RequestParam(required = false) String typeName) {
        List<YqZhuanTi> list = IYqZhuanTiService.selectYqZhuanTiTree(typeName);
        return success(list);
    }

    //通过pid获取树
    @PostMapping("/getTreeByPid")
    public AjaxResult getTreeById(Long id) {
        List<YqZhuanTi> list = IYqZhuanTiService.selectYqZhuanTiTreeByPid(id);
        return success(list);
    }


    //通过pid获取树
    @PostMapping("/getTree")
    public AjaxResult getTree(Long id) {
        List<YqZhuanTi> list = IYqZhuanTiService.getTree(id);
        return success(list);
    }


}
